<?php

/*
  [GRIRMS] (C) 2010 Grandison & JM Technologies

  @author kevin<hasson.lai@mobile-grandison.com>
  @version $Id: region/index.php 2 2010-09-13 $
 */


switch ($action) {
    case 'add':
        $parent_id = intval($parent_id);
        $parent_id < 1 && $parent_id = 1;
        $name = trim($category_name);
        $display_list = in_array($display_list,array('Y','N')) ? $display_list : 'Y';
        $display_menu = in_array($display_menu,array('Y','N')) ? $display_menu : 'Y';
        $module = trim($module);
        $page = trim($page);
        $parameter = trim($parameter);
        $link = trim($link);
        $order_by = intval($order_by);
        if((empty($module) && empty($page)) || file_exists(APP_ROOT."/module/{$module}/{$page}.php")){
            $sql = "INSERT INTO `system_category` (
                        `id` ,
                        `name` ,
                        `module` ,
                        `page` ,
                        `parameter` ,
                        `link` ,
                        `display_menu` ,
                        `display_list` ,
                        `parent_id` ,
                        `order_by`
                    )
                    VALUES (
                        NULL , '{$name}', '{$module}', '{$page}', '{$parameter}', '{$link}', '{$display_menu}', '{$display_list}', '{$parent_id}', '{$order_by}'
                    );";
            $db->query($sql);
            $data = $db->insert_id();
            $status = 1;
            $info = '添加成功';
            category_cache_update();
        }else{
            $status = 0;
            $info = '模块或页面不存在';
        }
        echo ajax_return($status, $info, $data);
        exit();
        break;
    case 'delete':
        $id = intval($id);
        if($id < 1){
            $status = 0;
            $info = 'no_id';
        }elseif ($id == 1){
            $status = 0;
            $info = 'root';
        }elseif ($db->result_first("SELECT COUNT(*) FROM system_category  WHERE parent_id='$id'") > 0) {
            $status = 0;
            $info = 'no_empty';
        }
        //elseif($db->result_first("SELECT COUNT(*) FROM XXX WHERE XX='{$id}'") > 0){
        //      $status = 0;
        //	$info = 'has_data';
        //}
        else{
            if($db->query("DELETE FROM system_category  WHERE id='$id'")){
                $status = 1;
                $info = 'ok';
                category_cache_update();
            }else{
                $status = 0;
                $info = 'ng';
            }
        }
        echo ajax_return($status, $info);
        exit();
        break;
    case 'edit':
        $data = array();
        $id = intval($category_id);
        $name = trim($category_name);

        if($id > 1){
            $display_list = in_array($display_list, array('Y','N')) ? $display_list : 'Y';
            $display_menu = in_array($display_menu, array('Y','N')) ? $display_menu : 'Y';
            $module = trim($module);
            $page = trim($page);
            $parameter = trim($parameter);
            $link = trim($link);
            $order_by = intval($order_by);
            
            if((empty($module) && empty($page)) || file_exists(APP_ROOT."/module/{$module}/{$page}.php")){
                $sql = "UPDATE system_category SET name='{$name}',module='{$module}',page='{$page}',parameter='{$parameter}',`link`='{$link}',display_menu='{$display_menu}',display_list='{$display_list}',order_by='{$order_by}' WHERE id='{$id}'";
                if($db->query($sql)){
                    $status = 1;
                    $info = '修改成功';
                    $data['id'] = $id;
                    $data['name'] = $name;
                    $data['display_list'] = $display_list;
                    $data['display_menu'] = $display_menu;
                    $data['module'] = $module;
                    $data['page'] = $page;
                    $data['parameter'] = $parameter;
                    $data['link'] = $link;
                    $data['order_by'] = $order_by;
                    category_cache_update();
                }else{
                    $status = 0;
                    $info = '修改失败';
                }
            }else{
                $status = 0;
                $info = '模块或页面不存在';
            }
        }elseif($id == 1){
            if($db->query("UPDATE system_category SET name='{$name}' WHERE id='1'")){
                $status = 1;
                $info = '修改成功，根分类只能修改名称';
                $data['id'] = 1;
                $data['name'] = $name;
                $data['display_list'] = 'N';
                $data['display_menu'] = 'N';
                $data['module'] = '';
                $data['page'] = '';
                $data['order_by'] = 0;
            }else{
                $status = 0;
                $info = '修改失败';
            }
        }else{
            $status = 0;
            $info = '请选择要修改的分类';
        }
        echo ajax_return($status, $info,$data);
        exit();
        break;
    case 'update':
        category_cache_update();
        echo ajax_return(1);
        exit();
        break;
    case 'get_page':
        $page_array = array();
        $page_path = APP_ROOT.'/module/'.trim($module_name);
        if(is_dir($page_path)){
            $page_dir = scandir($page_path);
            foreach($page_dir as $value){
                strpos($value, '.php') > 0 && $page_array[] = str_replace('.php', '', $value);
            }
            $status = 1;
            $info = 'OK';
        }else{
            $status = 0;
            $info = 'NG';
        }
        echo ajax_return($status, $info, $page_array);
        exit();
        break;
    default:
        $module_array = array();
        $module_path = APP_ROOT.'/module';
        if(is_dir($module_path)){
            $module_array = scandir($module_path);
        }
        break;
}

function category_cache_update() {
    global $category_list, $category_tree;
    $category_tree_data = category_cache_get_children(0, 1);
    cache_write('category_tree', $category_tree_data);
    $category_tree = $category_tree_data;
    $category_list_data = array();
    category_cache_list($category_list_data, $category_tree_data);
    cache_write('category_list', $category_list_data);
    $category_list = $category_list_data;
}

function category_cache_get_children($pid, $level) {
    global $db;
    $category_data = array();
    $result = $db->query("SELECT * FROM system_category WHERE parent_id='{$pid}'  ORDER BY order_by,id ASC");
    while ($row = $db->fetch_array($result)) {
        $this_category = $row;
        $this_category['childids'] = $row['id'];
        $this_category['children'] = category_cache_get_children($row['id'], $level + 1);
//        $this_category = array(
//            'id' => $row['id'],
//            'name' => $row['name'],
//            'parent_id' => $row['parent_id'],
//            'childids' => $row['id'],
//            'children' => category_cache_get_children($row['id'], $level + 1)
//        );
        foreach ($this_category['children'] as $child_category) {
            $this_category['childids'] .= ',' . $child_category['childids'];
        }
        $category_data[$row['id']] = $this_category;
    }
    return $category_data;
}

function category_cache_list(&$category_list_data, $category_tree_data) {
    foreach ($category_tree_data as $child_category) {
        $this_category = $child_category;
        unset($this_category['children']);
//        $this_category = array(
//            'id' => $child_category['id'],
//            'name' => $child_category['name'],
//            'parent_id' => $child_category['parent_id'],
//            'childids' => $child_category['childids']
//        );
        $category_list_data[$child_category['id']] = $this_category;
        if (!empty($child_category['children'])) {
            category_cache_list($category_list_data, $child_category['children']);
        }
    }
}

?>
